package com.fan.teamer.process;

import org.apache.log4j.Logger;

import com.fan.teamer.global.TeamerGlobalVariables;
import com.fan.teamer.model.domain.Member;
import com.fan.teamer.model.domain.Role;
import com.fan.teamer.service.MemberService;
import com.fan.teamer.service.RoleService;

public class MemberDeleteProcess extends ProcExec<Integer> {

	private static Logger logger = Logger.getLogger("MemberDeleteProcess");

	private Member member;
	private MemberService memberService;
	private RoleService roleService;

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		MemberDeleteProcess.logger = logger;
	}

	public Member getMember() {
		return member;
	}

	public void setMember(Member member) {
		this.member = member;
	}

	public MemberService getMemberService() {
		return memberService;
	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	public RoleService getRoleService() {
		return roleService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public int initCheck() {
		if (memberService == null || roleService == null) {
			setErrorMessage("Service is null!");
			return TeamerGlobalVariables.FAILED;
		}
		if (member == null) {
			setErrorMessage("Domain cannot be null!");
			return TeamerGlobalVariables.FAILED;
		}
		if (!memberService.hasMember(member.getEmail())) {
			setErrorMessage("Member: " + member.getEmail() + " doesn't exist!");
			return TeamerGlobalVariables.FAILED;
		}
		if (memberService.getJoinedTeams(member).size() > 0) {
			setErrorMessage("Member: " + member.getEmail()
					+ " joined teams, please quit team first!");
			return TeamerGlobalVariables.FAILED;
		}
		return TeamerGlobalVariables.SUCCESS;

	}

	@Override
	public Integer processImpl() {
		if (initCheck() == TeamerGlobalVariables.FAILED) {
			return TeamerGlobalVariables.FAILED;
		}
		Role role = roleService.getRole(member.getEmail());

		try {
			memberService.deleteMember(member);
			roleService.deleteRole(role);
		} catch (Exception e) {
			e.printStackTrace();
			setErrorMessage("Member: " + member.getEmail()
					+ " cannot be deleted!");
			return TeamerGlobalVariables.FAILED;
		}
		return TeamerGlobalVariables.SUCCESS;
	}

}
